import maya.cmds as mc
import maya.mel as mel
import sys
import re
def makeZeroGrp():
    sufname=mc.textFieldGrp('makeZeroGrpSuffix',q=True,text=True)
    seleobj=mc.ls(sl=True)
    for i in seleobj:
        groupname=str(i)+sufname
        mc.group(n=groupname,em=True)
        parent=mc.listRelatives(i,p=True)
        mc.parentConstraint(i,groupname,mo=False,n='needDeleteConstriant',w=1)
        mc.delete('needDeleteConstriant')
        mc.parent(i,groupname)
        if parent==None:
            pass
        else:
            mc.parent(groupname,parent)
####=======================================

##               ranameTools
#--------------------------------------------------------------------------
def addPrefixUI():
    prefixname=mc.textField('addPrefix',q=True,text=True)
    objsel=mc.ls(sl=True)
    print(prefixname)
    for i in objsel:
        mc.rename(i,prefixname+str(i))
def addSufixUI():
    Sufixname=mc.textField('addSufix',q=True,text=True)
    objsel=mc.ls(sl=True)
    for i in objsel:
        mc.rename(i,str(i)+Sufixname)
        
def searchReplaceNamesTool():
    buildOption=mc.radioButtonGrp('searchReplaceNames',q=True,sl=True)

    objSel=mc.ls(sl=True)
    if buildOption==1:
        mc.select(objSel,hi=True)
        objSel=mc.ls(sl=True)    
    searchname=mc.textField('Searchfor',q=True,text=True)
    replacename=mc.textField('ReplaceWith',q=True,text=True)
    print(objSel)
    for i in objSel:
        newname=str(i).replace(searchname,replacename)
        mc.rename(i,newname) 
        
def replaceName():
    sufix=mc.textField('repalcename',q=True,text=True)
    selename=mc.ls(sl=True)
    splitname=selename[0].split('_')
    newname=selename[0].replace(splitname[-1],sufix)
    mc.rename(selename[1],newname)
    
def SequenceRename():
    number=mc.intField('sequencerepalcename',q=True,value=True)
    numinword=[]
    selObj=mc.ls(sl=True)
    splitname=selObj[0].split('_')
    lensplitname=len(selObj)
    numWord=len(splitname[number])
    for k in range(numWord):
        try:
            result=int(splitname[number][k])
            print(result)
        except:
            result='none'
        if result!='none':
            numinword.append(k)
    lennuminword=len(numinword)
    if lennuminword==0:
        mc.error('the word selected has not a int type string')
    if lennuminword==1:
        needTrans=splitname[number][numinword[0]]
    if lennuminword>1:
        pre=numinword[0]
        suf=numinword[-1]+1
        needTrans=splitname[number][pre:suf]
    lenTrans=len(needTrans)
    if lenTrans>1:
        typenum=[]
        for j in range(lenTrans):
            splitTrans=needTrans[j]
            if int(splitTrans)!=0:
                typenum.append(j)
        lentypenum=len(typenum)
        if lentypenum==0:
            numberTrans=0
            preNum=[]
        else:
            numberTrans=needTrans[typenum[0]]
            preNum=needTrans[:typenum[0]]
    sequence=int(numberTrans)
    
    for i in range(lensplitname-1):
        sequence=sequence+1
        oriname=selObj[0]
        replacename=preNum+str(sequence)
        sequenceStr=str(replacename)
        newname=oriname.replace(needTrans,sequenceStr)
        mc.rename(selObj[i+1],newname)

def nameFieldWindow():
    nameFieldWindow='nameFieldWindow'
    if mc.window(nameFieldWindow,ex = True):
        mc.deleteUI(nameFieldWindow)
        
    mc.window(nameFieldWindow,widthHeight=(200, 300),t='name Field Window',menuBar = True,rtf=True,s=True)
    mc.rowColumnLayout('main',numberOfColumns=1,cal = (5,'center'),m=True)
    mc.rowColumnLayout('renameTools_01',numberOfColumns=2,cal = (5,'center'),m=True)
    selObj=mc.ls(sl=True)
    

    for i in selObj:
        mc.text(l=i)
        mc.nameField(o=i)

    mc.setParent('..')   

    mc.showWindow()
###==============================================================
#          getSkinJnt
#----------------------------------------------------------------

def getSkinJnt():
    SkinObj = mc.ls(sl=True)[0]
    skinCluster=mc.listConnections(str(SkinObj)+'.inMesh',s=True)[0]
    parenconnect=mc.listConnections(skinCluster,s=True,p=True)
    worldMatriaxAll=[]
    jntskinAll=[]
    for i in parenconnect:
        if 'worldMatrix' in i:
            worldMatriaxAll.append(i)
    for k in worldMatriaxAll:
        jntname=k.split('.')[0]
        jntskinAll.append(jntname)
    mc.select(jntskinAll)
###=================================================================
#         reverseSelect
#-------------------------------------------------------------------
def reverseSelect():
    objsle=mc.ls(sl=True)
    objsequence=objsle[::-1]
    mc.select(objsequence)
    currentsle=mc.ls(sl=True)
    sys.stdout.write(currentsle)